package com.lishe.lishe.lishe.module.recharge.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.bean.SpaceItemDecoration;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.recharge.adapter.RechargeRecordAdapter;
import com.lishe.lishe.lishe.module.recharge.bean.RechargeRecord;
import com.lishe.lishe.lishe.module.recharge.http.HttpPay;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.bean.RechargeRecordDataPageBody;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 2018/3/8.
 * 积分充值记录
 */

public class RechargeRecordActivity extends BaseActivity implements View.OnClickListener {

    public static void jump(Activity activity){
        activity.startActivity(new Intent(activity,RechargeRecordActivity.class));
    }

    private Button callBack;
    private TextView title;
    private RecyclerView listView;
    private TextView empty;
    private SmartRefreshLayout mSmartRefreshLayout;
    private RelativeLayout mRelativeEmpty;

    private List<RechargeRecord> mList;
    private RechargeRecordAdapter adapter;
    private int page = 1;
    private int totalPage = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_record);
        initView();
        setListener();
        setSmartRefreshLayout();
    }

    private void setListener() {
        callBack.setOnClickListener(this);
    }

    private void initView() {
        callBack = findViewById(R.id.title_left_button);
        title = findViewById(R.id.title_text_title);

        listView = findViewById(R.id.listView);
        mSmartRefreshLayout = findViewById(R.id.refreshlayout);
        mRelativeEmpty = findViewById(R.id.empty_layout);
        empty = findViewById(R.id.empty_text);

        listView.setLayoutManager(new LinearLayoutManager(this));
        listView.addItemDecoration(new SpaceItemDecoration(0,0,0, Utils.dip2px(this,10)));
        title.setText("点赞详情");
        empty.setText("暂无充值记录哦~");
    }

    private void setSmartRefreshLayout() {
        mList = new ArrayList<>();
        adapter = new RechargeRecordAdapter(mList);
        listView.setAdapter(adapter);

        mSmartRefreshLayout.autoRefresh(0, 1, 0.62f);
        mSmartRefreshLayout.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）
        mSmartRefreshLayout.setEnableLoadmore(false);

        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getRechargeRecord();
                mSmartRefreshLayout.setLoadmoreFinished(false);//将会再次触发加载更多事件
                if (!mSmartRefreshLayout.isEnableLoadmore())
                    mSmartRefreshLayout.setEnableLoadmore(true);
            }
        });

        mSmartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //未加载完，可以LoadMore
                if (page <= totalPage) {
                    mSmartRefreshLayout.setLoadmoreFinished(false);//将会再次触发加载更多事件
                    getRechargeRecord();
                } else {
                    mSmartRefreshLayout.finishLoadmore();
                    mSmartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
                }

            }
        });
    }

    private void getRechargeRecord() {
        HttpPay.getRechargeRecord(Globals.getToken(), page, 20, new DataResponseListener<RechargeRecordDataPageBody<RechargeRecord>>() {
            @Override
            public void onSuccess(RechargeRecordDataPageBody<RechargeRecord> dataResponse) {
                if (page == 1) {
                    mSmartRefreshLayout.finishRefresh(500, true);
                    mList.clear();
                } else {
                    mSmartRefreshLayout.finishLoadmore(500, true);
                }
                page++;
                totalPage = dataResponse.getTotalPage();
                mList.addAll(dataResponse.getInfo());

                if (mList.size() > 0) {
                    mRelativeEmpty.setVisibility(View.GONE);
                    adapter.notifyDataSetChanged();
                } else {
                    mRelativeEmpty.setVisibility(View.VISIBLE);
//                empty_text.setText(R.string.no_comment);
                    mSmartRefreshLayout.setEnableLoadmore(false);
                }
            }

            @Override
            public void onFail(String failMessage) {
                //网络差是刷新界面显示1秒钟后消失
                mSmartRefreshLayout.finishRefresh(1000, false);
                mSmartRefreshLayout.finishLoadmore(1000, false);

                toast(failMessage);
                if (mList.size() == 0) {
                    mSmartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
//                empty_text.setText(R.string.no_comment);
                    mRelativeEmpty.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(Throwable e) {
                //网络差是刷新界面显示1秒钟后消失
                mSmartRefreshLayout.finishRefresh(1000, false);
                mSmartRefreshLayout.finishLoadmore(1000, false);

                e.printStackTrace();
                toast(R.string.network_or_server_exception);
                if (mList.size() == 0) {
                    mSmartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
                    mRelativeEmpty.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_button:
                finish();
                break;
        }
    }
}
